package com.zhongmai.yzfgrabanorder.fragment;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.zhongmai.yzfgrabanorder.R;
import com.zhongmai.yzfgrabanorder.activity.HistoricalOrdersActivity;
import com.zhongmai.yzfgrabanorder.base.BaseFragment;
import com.zhongmai.yzfgrabanorder.netty_lib.netty.NettyClient;
import com.zhongmai.yzfgrabanorder.utils.Constants;
import com.zhongmai.yzfgrabanorder.utils.LogUtils;
import com.zhongmai.yzfgrabanorder.utils.Utils;
import com.zhongmai.yzfgrabanorder.view.LinkDialog;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 项目名称：YZFGrabAnOrder
 * 类描述：个人页面
 * 创建人：Administrator
 * 创建时间：2020/9/17 15:31
 * 修改人：Administrator
 * 修改时间：2020/9/17 15:31
 * 修改备注：
 */
public class PersonalFragment extends BaseFragment {
    @BindView(R.id.mBT_quit)
    Button mBT_quit;
    @BindView(R.id.mTV_user_mode)
    TextView mTV_user_mode;
    @BindView(R.id.mTV_user_id)
    TextView mTV_user_id;
    @BindView(R.id.mTV_user_copy)
    TextView mTV_user_copy;
    @BindView(R.id.mTV_historical_orders)
    TextView mTV_historical_orders;

    @Override
    protected int createSuccessView() {
        return R.layout.fragment_personal;
    }

    @Override
    protected void changeLayout() {

    }

    @Override
    protected void loadingData() {
        if (!TextUtils.isEmpty(Constants.user)) {
            mTV_user_id.setText(Constants.user);
        }
    }

    @Override
    protected void againLoad() {
        logMag("PersonalFragment-------againLoad");
    }

    @Override
    protected void switchFragemnt() {
        logMag("PersonalFragment-------switchFragemnt");
        if (mTV_title != null && mTV_title.getVisibility() == View.VISIBLE) {
            mTV_title.setText("个人中心");
        }
        updateOlineStatus();
    }

    /**
     * 更新用户状态
     */
    @SuppressLint("ResourceAsColor")
    private void updateOlineStatus() {
        if (NettyClient.isConnect) {
            mTV_user_mode.setBackground(getResources().getDrawable(R.color.tab_background));
            mTV_user_mode.setText("设备在线");
        } else {
            mTV_user_mode.setBackground(getResources().getDrawable(R.color.appdetail_video_seekbar_bg_one));
            mTV_user_mode.setText("设备下线");
        }
    }

    @OnClick({R.id.mBT_quit, R.id.mTV_user_copy, R.id.mTV_historical_orders})
    public void onViewClick(View view) {
        switch (view.getId()) {
            case R.id.mBT_quit:
                linkURLDialog();
                break;
            case R.id.mTV_user_copy:
                Utils.setClipboard(getContext(), mTV_user_id.getText().toString().trim());
                showToast("《设备ID》复制成功");
                break;
            case R.id.mTV_historical_orders:
                startActivity(new Intent(getContext(), HistoricalOrdersActivity.class));
                break;
            default:
                break;
        }
    }


    private void linkURLDialog() {
        LinkDialog linkDialog = new LinkDialog(getContext(), true, new LinkDialog.URLSuccess() {
            @Override
            public void success() {
                showToast("修改成功");
                logMag("url修改成功");
                Constants.manual_link = false;
                breakLongLink();
            }
        });
        linkDialog.show();
    }
}
